package com.example.plat.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Component
public class TimeFormateUtil {

    //年-月-日 上午/下午 时：分
    static SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    //年-月-日  时：分
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    //时：分
    static SimpleDateFormat timeFormate = new SimpleDateFormat("HH:mm");

    /**
     * 判断时间差
     *
     * @param dateStr:yyyy-MM-dd
     * @return
     */
    public boolean judgeRecent(String dateStr) {
        try {
            Date date = dateFormat.parse(dateStr);
            Date now = new Date();
            long diff = date.getTime() - now.getTime();
            double diffDays = diff * 1.0 / (24 * 60 * 60 * 1000);
            return diffDays < 3 && diffDays > 0 ? true : false;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 判断日期是否大于当前日期
     * @param date
     * @return
     * @throws ParseException
     */
    public static boolean judgeExpire(String date) throws ParseException {
        Date late = dateFormat.parse(date);
        Date now = new Date();
        return late.getTime() < now.getTime() ? true : false;
    }

    public static boolean judgeGreat(String u, String v) throws ParseException {
        Date d1 = dateFormat.parse(u);
        Date d2 = dateFormat.parse(v);
        return d1.getTime() <= d2.getTime();
    }

    //时间戳转日期
    public static String conversionTime(Long timeStamp) {
        //转换
        String time = dateF.format(new Date(timeStamp));
        return time;
    }

    /**
     * 时间转换成时间戳,参数是字符串,返回值是Int
     *
     * @param s: HH:mm
     * @return res
     * @throws ParseException
     */
    public static Integer timeToStamp(String s) throws ParseException {
        String[] time = s.split(":");
        int res = Integer.parseInt(time[0]) * 60 + Integer.parseInt(time[1]);
        return res;
    }

    /**
     * 将时间戳转换为时间,参数是字符串,返回值是String
     *
     * @param s
     * @return res
     */
    public static String stampToTime(Integer s) throws ParseException {
        String res;
        int hour = s / 60;
        int min = s % 60;
        res = hour < 10 ? "0" : "" + hour;
        res += min < 10 ? ":0" : ":" + min;
        return res;
    }


}
